function appBind() {
    var myTableBody = $(".myFollow-app-table>tbody");
    myTableBody.delegate(".glyphicon-plus", "click", function () {
        Aid = $(this).parent().parent().data("appid");
        $.post("/ajax/addToFollowApps", {AID: Aid}, function (data) {
            if (data === "OK") {
                queryFollow();
            }
        });
    });
}

function followBind() {
    var myTableBody = $(".myFollow-my-table>tbody");
    myTableBody.delegate(".glyphicon-remove", "click", function () {
        Aid = $(this).parent().parent().data("appid");
        $.post("/ajax/deleteFollowApps", {AID: Aid}, function (data) {
            if (data === "OK") {
                queryFollow();
            }
        });
    });
}

function specialApp() {
    var myTableBody = $(".myFollow-my-table>tbody");
    myTableBody.delegate(".glyphicon-star-empty", "click", function () {
        Aid = $(this).parent().parent().data("appid");
        $.post("/ajax/specialApp", {AID: Aid}, function (data) {
            if (data === "OK") {
                queryFollow();
            }
        });
    });
}

function queryFollow() {
    var username = $("#username").data("username");

    $.post("/ajax/getFollow", {username: username}, function (json) {
        var res = $.parseJSON(json);
        var total = res.total;
        var follows = res.follows;
        var followApp;
        var myTableBody = $(".myFollow-my-table>tbody");
        myTableBody.html("");
        for (i = 0; i < total; i++) {
            followApp = $('<tr><td>' + follows[i].name + '</td><td><span class="glyphicon glyphicon-remove"></span><span class="glyphicon glyphicon-star-empty"></span></td></tr>');
            if(follows[i].AID === res.SpAID ){
                followApp.find(".glyphicon-star-empty").removeClass("glyphicon-star-empty").addClass("glyphicon glyphicon-star");
            }
            followApp.data("appid", follows[i].AID);
            myTableBody.append(followApp);
        }
    });
}

function init() {
    Aid = 0;
}

$(function () {
    init();
    queryFollow();
    followBind();
    appBind();
    specialApp()
});